home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / cntrl0.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  8.4 KB  |  170 lines

  1. // CNTRL0.CPP - "driver" function for RARS - M. Timin, Feb. 1995
  2. // adapted to ver. 0.39 3/6/95 by M. Timin
  3.  
  4.              /* Easy-To-Understand Robot Driver */
  5. /*
  6. This robot driver attempts to stay in the middle of the track at all
  7. times.  She calculates a cornering speed for each corner based on its
  8. radius.  She accelerates on each straightaway until a certain fraction
  9. of its length is reached; Then she slows down, attempting to arrive at
  10. the corner with the proper cornering speed.  In the corner she attempts
  11. to maintain the cornering speed while attempting to stay in the middle
  12. of the track.  Her strategy for passing is to choose right or left at
  13. random, and then throw the car into a sharp slide toward that direction.
  14. (pretty ballsy, huh!)
  15. */
  16. // The language here can be considered to be ANSI C or C++.
  17.  
  18. #include <string.h>
  19. #include <stdlib.h>
  20. #include <math.h>
  21. #include "car.h"
  22.  
  23. // These parameters may be adjusted to get better performance:
  24. const double CORN_SPD_CON = 6.2;   // determines how fast to take corners
  25. const double STEER_GAIN = 2.5;     // servo gain, for staying in "lane"
  26. const double STEER_DAMP = 1.5;     // servo damping, to prevent "weaving"
  27. const double END_ACCEL = .25; // we accelerate until this fraction of length
  28. const double END_CORNER = 2.5;// used to decide when to start leaving the corer
  29. const double ESC_ALPHA = .03; // after a crash, set alpha like this, + or -
  30. const double SLIP_LIM = 3.0;  // maximum wheel slip, ft/sec, in wheel_slip()
  31. const double SLIP_CON = 70.0; //
  32. const double SHARP_TURN = .1; // change in alpha when attempting to pass
  33. const int PASSING_TIME = 50;  // time to stay in passing maneuver, counts
  34.  
  35. extern char* glob_name;       // The name string, below, will be copied here
  36.  
  37. // The following function calculates the speed for a corner.
  38. // The lateral force produced by cornering is proportional to the square
  39. // of the speed, and is inversely proportional to the radius of the path.
  40. // Therefore, the attainable cornering speed for differnt radii is
  41. // proportional to the square root of the radius.  This function implements
  42. // that rule.  The value to use for CORN_SPD_CON can be determined by
  43. // trial and error.  Example result: if the car is following a path with
  44. // radius of 100 ft, and if CORN_SPD_CON is 5.0, then corn_speed is 50 ft/sec.
  45. double corn_speed(double radius)
  46. {
  47.    if(radius < 0.0)         // change sign of negative radius
  48.       radius = -radius;
  49.    else if(radius == 0.0)   // This is just insurance, this funtion doesn't
  50.       return(200.0);        // make sense when the radius is zero.
  51.    return CORN_SPD_CON * sqrt(radius);
  52. }
  53.  
  54. // In order to set vc, if you know how fast you want to go (goal), and how
  55. // fast you are going now (present), This function will compute a reasonable
  56. // value for vc.  The value is never very far from the present speed, both
  57. // to attempt to stay within the power limit, and to maintain steering control.
  58. // You can adjust the resulting slip by changing SLIP_LIM.
  59. double wheel_speed(double goal, double present)
  60. {
  61.    double ws;
  62.  
  63.    if(present > goal + 2 * SLIP_LIM)  // if too fast,
  64.       ws = present - SLIP_LIM;      // slow down.
  65.    else if(present < goal - 2 * SLIP_LIM)  // if too slow,
  66.       ws = present + SLIP_LIM;             // accelerate.
  67.    else                           // if quite close,
  68.       ws = (goal + present) / 2;      // approach desired speed gently.
  69.  
  70.    return ws;
  71. }
  72.  
  73. /* These two structures from CAR.H are repeated here as comments, because
  74.    the "driver" function receives situation as input and produces con_vec
  75.    as output.
  76. struct situation {       // a car's local situation as seen by the driver
  77.    double cur_rad;       // radius of inner wall of curve (0 means straight)
  78.    double cur_len;       // length of current track segment (angle if curve)
  79.    double to_lft;        // distance to left wall
  80.    double to_rgt;        // distance to right wall
  81.    double to_end;        // how far to end of current track seg. (angle or feet)
  82.    double v;             // the speed of the car, feet per second
  83.    double vn;            // component of v perpendicular to track direction
  84.    double nex_len;       // length of the next track segment (angle if curve)
  85.    double nex_rad;       // radius of inner wall of next segment (or 0)
  86.    double after_rad;     // radius of the segment after that one. (or 0)
  87.    double power_req;     // ratio: power requested by driver to maximum power
  88.    int dead_ahead;       // set when there is a car dead ahead, else 0
  89. };
  90. struct con_vec { double alpha, vc; };  // control vector, steering & throttle
  91. */
  92.  
  93. // The task of this function is to compute vc and alpha.  A high speed
  94. // car on a track is a little like the keel of a boat; if you set the keel
  95. // at a slight angle to the direction of the oncoming water, you get a large
  96. // force to the side.  That is how we corner the car.  The driver sets the
  97. // car at a slight angle with respect to its direction of motion, this
  98. // cause a force to the side, causing the path of the car to curve.  The
  99. // magnitude of the force is proportional to the angle (alpha) for very
  100. // small alpha, and when there is not much wheel spin.  The wheel spin
  101. // is controlled by vc, which is the rearward speed of the bottom of the
  102. // tire.  When going down the straight at a constant, moderate velocity,
  103. // then vc is equal to the speed of the car.  For acceleration, vc is
  104. // made a little greater than the speed.  For braking, it is made a little
  105. // less.  When accelerating, vc is limited by the power available.
  106. con_vec cntrl0(situation s)
  107. {
  108.    const char name[] = "Annie";      // This is the robot driver's name!
  109.    static int init_flag = 1;          // cleared by first call
  110.    double speed;                      // target speed for cornering, ft/sec
  111.    double speed_next;                 // target speed for next corner
  112.    con_vec result;                    // This is what is returned.
  113.    double width;                      // track width, feet
  114.    double alpha, vc;           // components of result
  115.    static double alpha_inc = 0.0;  // alpha increment during passing maneuver
  116.    static int counting = 0;    // will be set and counting down when passing
  117.    // This paragraph has nothing to do with car control; it is just
  118.    // to identify the driver by copying its name to a global RAM area:
  119.    // This happens only on the very first call to this function
  120.  
  121.    if(init_flag)  {            // first time through, only copy name:
  122.       strcpy(glob_name, name);
  123.       init_flag = 0;
  124.       result.alpha = result.vc = 0;
  125.       return result;
  126.    }
  127.  
  128.   if(stuck(s.backward, s.v,s.vn, s.to_lft,s.to_rgt, &result.alpha,&result.vc))
  129.       return result;
  130.  
  131.    // Set alpha based on a servo-mechanism approach, trying to stay
  132.    // in the middle of the track, i.e., s.to_left equal to .5 * width:
  133.    width = s.to_lft + s.to_rgt;                        // find width of track
  134.    alpha = STEER_GAIN * (s.to_lft - .5 * width) / width;
  135.    alpha -= STEER_DAMP * s.vn / s.v;  // This is damping, to prevent oscillation
  136.  
  137.    // calculate target speeds for current corner and the next:
  138.    speed = corn_speed(s.cur_rad + .5 * width);      // speed is based on radius
  139.    speed_next = corn_speed(s.nex_rad + .5 * width); // of center line of track.
  140.  
  141.    // now set the tire speed, vc:
  142.    if(s.cur_rad == 0.0)                  // If we are on a straightaway,
  143.       if(s.to_end > END_ACCEL * s.cur_len) // if we are far from the end,
  144.          vc = s.v + SLIP_CON / s.v;          // keep accellerating near full power
  145.       else                    // otherwise,
  146.          vc = wheel_speed(speed_next, s.v);    // brake for next corner
  147.    else                              // If we're in the curve, maintain speed.
  148.       if(s.to_end * (s.cur_rad + .5 * width) > END_CORNER * width)
  149.                   // if we are far from the next corner, stay at "speed".
  150.          vc = wheel_speed(speed, s.v);
  151.       else        // but when we near the next corner, adjust to "speed_next"
  152.          vc = wheel_speed(speed_next, s.v);
  153.  
  154.    // The passing maneuver:
  155.    if(s.dead_ahead & !counting)  {  // When first encountering the car ahead:
  156.       counting = PASSING_TIME;         // setup the timer,
  157.       if(random(29999) < 15000)        // choose a right or left maneuver:
  158.          alpha_inc = SHARP_TURN;
  159.       else
  160.          alpha_inc = -SHARP_TURN;
  161.    }
  162.    if(counting)  {                // If we are still in the passing maneuver,
  163.       alpha += alpha_inc;              // change alpha
  164.       --counting;                      // count down to zero
  165.    }
  166.  
  167.    result.vc = vc;   result.alpha = alpha;
  168.    return result;
  169. }
  170.